package com.zkzx.freight.bill.service;

import com.zkzx.common.freight.command.OrderCommand;
import com.zkzx.common.freight.model.OrderItemVO;
import com.zkzx.freight.bill.mapper.OrderItemMapper;
import com.zkzx.freight.bill.mapper.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Optional;

/**
 * 北京中科智兴物联科技有限公司 @版权所有
 *
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/12/18:13
 * @Description:
 */
@Slf4j
@Service
public class OrderItemService {

    @Resource
    private OrderItemMapper orderItemMapper;

    /**
     * 根据订单id查询货物信息
     * @param orderId 订单id
     * @return 货物信息
     */
    public OrderItemVO findVOByOrderIdId(Integer orderId){
        OrderItem orderItem = findByOrderId(orderId);
        if (Objects.isNull(orderItem)){
            return null;
        }
       return OrderItemVO.builder()
               .id(orderItem.getId())
               .orderId(orderItem.getOrderid())
               .productName(orderItem.getProductname())
               .productWeight(orderItem.getProductweight())
               .productPackag(orderItem.getProductpackag())
               .productSort(orderItem.getProductsort())
               .producTbulk(orderItem.getProductbulk())
               .standardLong(orderItem.getStandardlong())
               .standardWidth(orderItem.getStandardwidth())
               .standardHigh(orderItem.getStandardhigh())
               .build();
    }

    /**
     * 根据订单id查询货物信息
     * @param orderId 订单id
     * @return 货物信息
     */
    public OrderItem findByOrderId(Integer orderId){
        OrderItemExample orderItemExample = new OrderItemExample();
        orderItemExample.createCriteria().andOrderidEqualTo(orderId);
        Optional<OrderItem> orderItemOne = orderItemMapper.selectByExample(orderItemExample).stream().findFirst();

        OrderItem orderItem=null;
        if (orderItemOne.isPresent()){
            orderItem=orderItemOne.get();
        }
        return orderItem;
    }

    /**
     * 插入货物信息
     * @param command 入参
     * @param id 订单id
     */
    public void insert(OrderCommand command, Integer id){
        if(Objects.isNull(command.getOrderItemVO())){
            return;
        }
        OrderItem orderItem = new OrderItem();
        orderItem.setOrderid(id);
        Optional.ofNullable(command.getOrderItemVO()).ifPresent(oi->{
            orderItem.setProductname(command.getOrderItemVO().getProductName());
            orderItem.setProductweight(command.getOrderItemVO().getProductWeight());
            orderItem.setProductpackag(command.getOrderItemVO().getProductPackag());
            orderItem.setProductsort(command.getOrderItemVO().getProductSort());
            orderItem.setProductbulk(command.getOrderItemVO().getProducTbulk());
            orderItem.setStandardlong(command.getOrderItemVO().getStandardLong());
            orderItem.setStandardwidth(command.getOrderItemVO().getStandardWidth());
            orderItem.setStandardhigh(command.getOrderItemVO().getStandardHigh());

        });
        orderItemMapper.insertSelective(orderItem);
    }



    /**
     * 更新货物信息
     * @param command 入参
     * @param id 订单id
     */
    public void update(OrderCommand command, Integer id){
        if(Objects.isNull(command.getOrderItemVO())){
            return;
        }
        OrderItem orderItem = new OrderItem();
        orderItem.setOrderid(id);
        orderItem.setProductname(command.getOrderItemVO().getProductName());
        orderItem.setProductweight(command.getOrderItemVO().getProductWeight());
        orderItem.setProductpackag(command.getOrderItemVO().getProductPackag());
        orderItem.setProductsort(command.getOrderItemVO().getProductSort());
        orderItem.setProductbulk(command.getOrderItemVO().getProducTbulk());
        orderItem.setStandardlong(command.getOrderItemVO().getStandardLong());
        orderItem.setStandardwidth(command.getOrderItemVO().getStandardWidth());
        orderItem.setStandardhigh(command.getOrderItemVO().getStandardHigh());

        OrderItemExample orderItemExample = new OrderItemExample();
        OrderItemExample.Criteria criteria = orderItemExample.createCriteria();
        criteria.andOrderidEqualTo(id);
        orderItemMapper.updateByExampleSelective(orderItem,orderItemExample);


    }
}
